package com.example.chat2.controller;

import com.example.chat2.common.Result;

import com.example.chat2.domain.TbUserinfo;

import com.example.chat2.service.UserinfoService;
import com.example.chat2.vo.AuthUserVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Controller
@RestController
@RequestMapping("/userinfo")

public class UserinfoController {
    @Autowired
    UserinfoService userinfoService;


    private static final Logger logger = LoggerFactory.getLogger(UserinfoController.class);



    /*
     *查询所有数据
     * */
    @GetMapping(value = "/getUserinfoList", produces = "application/json")
    public Result getUserinfoList(){
        String accept = "Accept";
        logger.info("Received Accept header: {}", ("Accept"));

        List<TbUserinfo> list=  userinfoService.getUserinfosList();
        return Result.OK("成功",list );


    }


    @GetMapping ("/getUserinfoById")
    public Result  getUserinfoById( @RequestParam("id") Long  id){
        TbUserinfo userinfo =  userinfoService.getUserinfoById(id);
        return Result.OK("成功",userinfo);
    }

    @PostMapping( "/login")
    public Result login(@RequestBody AuthUserVo authUserVo) {
        TbUserinfo userinfo=userinfoService.login(authUserVo);
        if (userinfo==null){
            return Result.FAIL("用户名或者密码错误");
        }else {
            return Result.OK("查询用户数据成功", userinfo);
        }
    }



    @PostMapping("/register")
    public  Result    saveUserinfo( @RequestBody TbUserinfo  userinfo){
        userinfoService.register(userinfo);
        return Result.OK("成功",null);
    }






}
